<template>
  <div class="app-container">
    <!-- 添加或修改项目基础信息管理对话框 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="框架单号" prop="projectCode">
        <el-input v-model="queryParams.params['projectCode']" placeholder="请输入框架单号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订单编号" prop="projectNo">
        <el-input v-model="queryParams.projectNo" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="工程名称" prop="gongchengName">
        <el-input v-model="queryParams.gongchengName" placeholder="请输入工程名称" clearable
                  @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品编码" prop="goodsCode">
        <el-input v-model="queryParams.goodsCode" placeholder="请输入产品编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品名称" prop="goodsName">
        <el-input v-model="queryParams.goodsName" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品型号" prop="model">
        <el-input v-model="queryParams.model" placeholder="请输入产品型号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="下单日期" prop="orderDate">
        <el-date-picker clearable v-model="queryParams.params['orderDate']" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择下单日期">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="下单日期">
        <el-date-picker v-model="dateRange"
                        style="display: flex;"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="业务员" prop="projectNo">
        <el-input v-model="queryParams.params['salesmanName']" placeholder="请输入业务员" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="制单人" prop="createName">
        <el-input v-model="queryParams.params['createName']" placeholder="请输入制单人" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="合同收货人" prop="deliveryPerson">
        <el-input v-model="queryParams.deliveryPerson" placeholder="请输入制单人" clearable
                  @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="是否自制" prop="isdo">
        <el-select v-model="queryParams.isdo" placeholder="是否自制" clearable style="width: 130px">
          <el-option v-for="dict in dict.type.isdo" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
     <!-- <el-form-item label="是否拆解" prop="ischaijie">
        <el-select v-model="queryParams.ischaijie" placeholder="是否拆解" clearable style="width: 130px">
          <el-option v-for="dict in dict.type.ischaijie" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>-->
      <!--<el-form-item label="工序分配状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="工序分配状态" clearable style="width: 130px">
          <el-option v-for="dict in dict.type.sale_state" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExportDetail">导出
        </el-button>
      </el-form-item>
    </el-form>
    <!--<el-table :data="dataList" :max-height="tableMaxHeight" @selection-change="handleSelectionChange"
              v-loading="loading">-->
    <el-table border :data="columns" ref="dragTable" row-key="id" v-loading="loading"
      :max-height="tableMaxHeight" @sort-change="handleSortChange" show-summary :summary-method="getSummaries">
      <el-table-column align="center" type="selection" width="55" />
      <el-table-column align="center" label="序号" type="index" width="50">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="请购单号" align="center" prop="lvdoNo" sortable />
      <el-table-column label="框架编号" align="center" prop="order.projectCode" show-overflow-tooltip/>
      <el-table-column label="订单编号" align="center" prop="projectNo" sortable />
      <el-table-column label="创建日期" align="center" prop="createTime" sortable show-overflow-tooltip />
      <el-table-column label="工程名称" align="center" prop="gongchengName" show-overflow-tooltip sortable />
      <!--<el-table-column label="产品编码" align="center" prop="goodsCode" sortable>
        <template slot-scope="scope">
            <span>{{ scope.row.goodsCode }}</span>
        </template>
      </el-table-column>-->
      <el-table-column sortable align="center"  label="产品编码" prop="goodsCode">
        <template slot-scope="scope">
          <el-tooltip class="item" content="点击查看详情" effect="dark" placement="left-start">
            <span @click="showPuduceSaleAll(scope.row)" style="color: #00afff;cursor:pointer;">
              {{scope.row.goodsCode}}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="产品状态" align="center" prop="status" sortable>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.goods_status" :value="scope.row.status" />
        </template>
      </el-table-column>
     <!-- <el-table-column align="center" label="工序分配状态" prop="state" sortable>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sale_state" :value="scope.row.state" />
        </template>
      </el-table-column>-->
      <el-table-column align="center" label="产品名称" prop="goodsName" sortable />
      <el-table-column align="center" label="合同物料名称" prop="contractGoodsName" sortable />
      <el-table-column align="center" label="合同收货人" prop="deliveryPerson" sortable />
      <el-table-column align="center" label="产品型号" prop="model" sortable />
      <el-table-column align="center" label="产品单位" prop="unit" sortable />
      <el-table-column align="center" label="销售单价" prop="goodsUnitPrice" sortable />
      <el-table-column align="center" label="销售总价" prop="goodsTotalPrice" sortable />
      <el-table-column align="center" label="业务员" prop="order.salesmanName"  />
      <el-table-column align="center" label="制单人" prop="order.createName"  />
      <el-table-column align="center" label="订单数量" prop="orderCount" sortable />
      <el-table-column align="center" label="锁定数量" prop="lockCount" sortable />
      <el-table-column label="是否自制" align="center" prop="isdo" sortable>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.isdo" :value="scope.row.isdo" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="自制/请购数量" prop="pleaseBuyCount" sortable />
      <!--<el-table-column label="巡检单" align="center" prop="xunjianFile" width="200" sortable>
        <template slot-scope="scope" v-if="scope.row.xunjianFile">
          <image-preview :src="scope.row.xunjianFile" height="60px" width="40px"></image-preview>
        </template>
      </el-table-column>-->
      <el-table-column align="center" label="备注" prop="remark" sortable />
      <!--<el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button @click="chaijie(scope.row)" icon="el-icon-view" size="mini" type="text">
            串行拆解
          </el-button>
          <el-button @click="xunjian(scope.row)" icon="el-icon-view" size="mini" type="text">
            巡检单
          </el-button>
        </template>
      </el-table-column>-->
    </el-table>
    <choseGx-form @ok="getList" ref="choseGxForm"></choseGx-form>
    <chajie-form @ok="getList" ref="chaijieForm"></chajie-form>
    <fpTaskForm-list @ok="getList" ref="taskFpForm"></fpTaskForm-list>
    <xunjian-form @ok="getList" ref="xunjianForm"></xunjian-form>
    <showPuduceSaleAll-form @ok="getList" ref="showPuduceSaleAllForm"></showPuduceSaleAll-form>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>

  import {deleteAction, downloadFile, getAction,accAdd} from '@/api/manage'
  import ChoseGxForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/ChoseGxForm";
  import ChajieForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/ChajieForm";
  import FpTaskFormList from "@/views/develop/projectBaseInfo/prjForGoodsIn/FpTaskFormList";
  import XunjianForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/XunjianForm";
  import ShowPuduceSaleAllForm from "@/views/develop/projectBaseInfo/ShowPuduceSaleAllForm";
  import { getActionList } from "@/api/develop/goodsSaledo";
  export default {
    name: "ProjectSaleList",
    dicts: ['isdo', 'sale_state', 'goods_status','ischaijie'],
    components: {
      ChoseGxForm,
      ChajieForm,
      FpTaskFormList,
      XunjianForm,
      ShowPuduceSaleAllForm,
    },
    props: {
        projectCode: {
            type: [String],
            required: true,
            default: ''
        },
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        activeName: "first",
        // 弹出层标题
        title: "设定产品工序",
        // 是否显示弹出层
        open: false,
        // 显示搜索条件
        showSearch: true,
        // 表单参数
        form: {},
        proId: "",
        proStatus: "",
        columns: [],
        total: 0,
        // 表单校验
        rules: {},
        dateRange: [],
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          params: {},
          id: null,
          code: null,
          projectCode: null,
          projectNo: null,
          gongchengName: null,
          deliveryPerson: null,
          goodsName: null,
          goodsCode: null,
          model: null,
          brand: null,
          isdo: null,
          ischaijie: null,
          remark: null,
          projectId: null,
          orderByColumn:"sort",
          isAsc:"asc"
        },
        url: {
          list: '/goodsSale/goodsSale/list',
          getById: '/projectBaseInfo/projectBaseInfo/',
          exportData: '/goodsSale/goodsSale/exportData',
        },
        tableMaxHeight: window.innerHeight - 320,
      };
    },
    watch: {
        'projectCode': {
            handler(newVal, oldVal) {
              console.log("projectCode", newVal);
                if (newVal) {
                    this.queryParams.projectNo = newVal;
                    this.getList();
                }
            },
            deep: true,
            immediate: true
        },
    },
    created() {
      //this.handleQuery();
      this.getList();
      window.addEventListener('resize', this.getTableHeight);
    },
    methods: {
      showPuduceSaleAll(row){
        this.$refs.showPuduceSaleAllForm.show(row);
      },
      getSummaries(param) {
        // console.log(param);
        const {columns, data} = param;
        const sums = [];
        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = "合计";
            return;
          }
          const values = data.map((item) => Number(item[column.property]));
          // 只合计amount1和amount3，想合并哪些列就在这里指定
          if ( column.property === "goodsUnitPrice" || column.property === "goodsTotalPrice"  ||  column.property === "orderCount" || column.property === "lockCount" || column.property === "pleaseBuyCount") {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return accAdd(prev , curr);
              } else {
                return prev;
              }
            }, 0);
          }
        });
        return sums;
      },
      xunjian(row) {
        this.$refs.xunjianForm.xunjianShow(row);
      },
      taskFp(row) {
        this.$refs.taskFpForm.taskFpShow(row);
      },
      chaijie(row) {
        this.$refs.chaijieForm.chaijieGoods(row);
      },
      choseGy(row) {
        this.$refs.choseGxForm.choseGy(row);
      },
      getList() {
        this.loading = true;
        getActionList(this.addDateRange(this.queryParams, this.dateRange,"OrderDate")).then(response => {
          this.columns = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = []
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      reset() {
        this.form = {
          id: null,
          remark: null,
          delFlag: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      handleSortChange(column) {
        const { prop, order } = column;
        this.queryParams.orderByColumn = prop;
        this.queryParams.isAsc = order === 'ascending' ? 'asc' : 'desc';
        this.handleQuery();
      },
      handleExportDetail(row) {
        downloadFile(this.url.exportData, this.queryParams);
      },


    }
  };
</script>
